package com.iotteam.androidbase.commen.views.charts;

import android.graphics.Paint;

import java.net.PortUnreachableException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by vonchenchen on 14/08/2017.
 */

public class Axis {

    /** 轴距离左边距离占总宽度的百分比 */
    private double mAxisMarginLeftRatio = 0.15;
    /** 轴距离下边距离占总宽度的百分比 */
    private double mAxisMarginBottonRatio = 0.15;

    private double mAxisMarginLeft = mAxisMarginLeftRatio;
    private double mAxisMarginBotton = mAxisMarginBottonRatio;

    /** 最大数值 */
    private double mMax;
    /** 最小数值 */
    private double mMin;
    /** 切分份数 */
    private double mDevide;
    /** 每个刻度的实际长度 */
    private double mGap;
    /** 轴线实际长度 */
    private double mTotleLen;

    private Point mBaseLineStartPoint;
    private Point mBaseLineStopPoint;

    private Point mLeftTop;
    private Point mRightBotton;

    private List<Point> mDevidePointList;
    private List<String> mTextList;

    private Oritation mOritation;

    private AxisComputerInfo mAxisComputerInfo;
    private final double mMarginLeft;
    private final double mMarginBotton;

    public enum Oritation{
        Horizantal,
        Vertical
    }

    public Axis(double max, double min, int devide, Oritation oritation, Point leftTop, Point rightBotton){
        mMax = max;
        mMin = min;
        mDevide = devide;

        mOritation = oritation;

        mBaseLineStartPoint = new Point();
        mBaseLineStopPoint = new Point();

        mLeftTop = leftTop;
        mRightBotton = rightBotton;

        mMarginLeft = mAxisMarginLeft * rightBotton.x;
        mMarginBotton = mAxisMarginBotton * rightBotton.y;

        if(oritation == Axis.Oritation.Vertical){

            //纵轴计算纵轴间隔
            mGap = Math.abs(rightBotton.y - mMarginBotton)/devide;
            mTotleLen = Math.abs(rightBotton.y - mMarginBotton);

            mBaseLineStartPoint.x = mMarginLeft;
            mBaseLineStartPoint.y = 0;

            mBaseLineStopPoint.x = mBaseLineStartPoint.x;
            mBaseLineStopPoint.y = rightBotton.y * (1 - mAxisMarginBotton);

        }else if(oritation == Axis.Oritation.Horizantal){

            //横轴计算横坐标间隔
            mGap = Math.abs(rightBotton.x - mMarginLeft)/devide;
            mTotleLen = Math.abs(rightBotton.x - mMarginLeft);

            mBaseLineStartPoint.x = mMarginLeft;
            mBaseLineStartPoint.y = rightBotton.y * (1 - mAxisMarginBotton);

            mBaseLineStopPoint.x = rightBotton.x;
            mBaseLineStopPoint.y = mBaseLineStartPoint.y;
        }

        mDevidePointList = new ArrayList<>();

        int pointNumer = devide + 1;

        for(int i=0; i<pointNumer; i++){
            Point point = new Point();
            if(oritation == Axis.Oritation.Vertical){
                point.x = getMarginLeft();
                point.y = i * mGap;
            }else if(oritation == Axis.Oritation.Horizantal){
                point.x = getMarginLeft() + i * mGap;
                point.y = mBaseLineStartPoint.y;
            }
            mDevidePointList.add(point);
        }

        mAxisComputerInfo = new AxisComputerInfo();
        mAxisComputerInfo.mDevide = mDevide;
        mAxisComputerInfo.mGap = mGap;
        mAxisComputerInfo.mMax = mMax;
        mAxisComputerInfo.mMin = mMin;
        mAxisComputerInfo.mTotleLen = mTotleLen;
        if(oritation == Axis.Oritation.Vertical) {
            mAxisComputerInfo.mMargin = mMarginLeft;
        } else if(oritation == Axis.Oritation.Horizantal){
            mAxisComputerInfo.mMargin = mMarginBotton;
        }
    }

    public double getMarginLeft(){
        //return (mRightBotton.x - mLeftTop.x) * mAxisMarginLeftRatio;
        return mMarginLeft;
    }

    public double getLineLength(){
        return (mRightBotton.x - mLeftTop.x);
    }

    public AxisComputerInfo getAxisComputerInfo(){
        return mAxisComputerInfo;
    }

    public double getMarginBotton(){
        //return (mRightBotton.y - mLeftTop.y) * mAxisMarginBottonRatio;
        return mMarginBotton;
    }

    /**
     * 刻度列表
     * @param list
     */
    public void setTextList(List<String> list){
        mTextList = list;
    }

    public List<String> getTextList(){
        return mTextList;
    }

    public List<Point> getDevidePointList(){
        return mDevidePointList;
    }

    /**
     * 左上角坐标
     * @return
     */
    public Point getBaseLineStartPoint(){
        return mBaseLineStartPoint;
    }

    /**
     * 右下角坐标
     * @return
     */
    public Point getBaseLineStopPoint(){
        return mBaseLineStopPoint;
    }

    public Oritation getOritation(){
        return mOritation;
    }

    public static class AxisComputerInfo{
        /** 最大数值 */
        public double mMax;
        /** 最小数值 */
        public double mMin;
        /** 切分份数 */
        public double mDevide;
        /** 每个刻度的实际长度 */
        public double mGap;
        /** 轴线实际长度 */
        public double mTotleLen;
        /** 实际边距长度 */
        public double mMargin;
    }
}
